package com.bodu.global.security;

import com.bodu.system.entity.po.PermitPo;
import com.bodu.system.entity.po.RolePo;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author gaoya
 * Description 登录用户信息
 * @date 2022 - 10 - 10 23:27:33
 */
@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private Long id;

    private String username;

    private String password;

    /**
     * 头像查询
     */
    private String url;

    private List<RolePo> rolePos;

    private List<PermitPo> permitPos;

    /**
     * 用户客户管理的单位
     */
    private Set<Long> managerDept;

    public LoginUser(Long id, String username, String password, List<RolePo> rolePos, List<PermitPo> permitPos) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.rolePos = rolePos;
        this.permitPos = permitPos;
    }

    public LoginUser(Long id, String username, String password, String url, List<RolePo> rolePos, List<PermitPo> permitPos) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.url = url;
        this.rolePos = rolePos;
        this.permitPos = permitPos;
    }

    public LoginUser(Long id, String username, String password, String url, List<RolePo> rolePos, List<PermitPo> permitPos, Set<Long> managerDept) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.url = url;
        this.rolePos = rolePos;
        this.permitPos = permitPos;
        this.managerDept = managerDept;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = Lists.newArrayList();
        permitPos.forEach(permit -> {
            if (!Strings.isNullOrEmpty(permit.getAuthority())) {
                list.add(new SimpleGrantedAuthority(permit.getAuthority()));
            }
        });
        return list;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
